package com.hk.controller;

import com.github.pagehelper.PageInfo;
import com.hk.commons.CommonResult;
import com.hk.model.Product;
import com.hk.model.vo.Result;
import com.hk.model.vo.ResultVO;
import com.hk.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @Author: aerfazhe
 * @Date: 2021/7/7 15:21
 * @Statement: 产品
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService iProductService;

    @GetMapping("/findAll")
    public ResultVO findAll(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10")Integer limit,
            Product product
    ) {
        List<Product> productList = iProductService.findAll(page, limit, product);
        PageInfo<Product> productPageInfo = new PageInfo<>(productList);
        int count = iProductService.count(-1,-1, product);
        if (productList.isEmpty()) {
            ResultVO resultVO = CommonResult.nullResultVO(count, productPageInfo.getList());
            return resultVO;
        }
        ResultVO resultVO = CommonResult.successResultVO(count, productPageInfo.getList());
        return resultVO;
//        return null;
    }

    @PostMapping("/save")
    public Result save(@RequestBody(required = false) Product product) {
        if (product == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        int save = iProductService.save(product);
        Result result = CommonResult.successResult(null);
        return result;
//        return null;
    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable int id) {
        Product product = iProductService.findById(id);
        if (product.getId() == 0) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        Result result = CommonResult.successResult(product);
        return result;
//        return null;
    }

    @PostMapping("/updateMessage")
    public Result updateMessage(@RequestBody(required = false) Product product) {
        if (product == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        int update = iProductService.update(product);
        Result result = CommonResult.successResult(null);
        return result;
    }

    /**
     * 通过Id所属产品
     * @param id
     * @return
     */
    @PostMapping("/deletetProduct/{id}")
    public Result deletetProduct(@PathVariable int id) {
        int delete = iProductService.delete(id);
        Result result = CommonResult.successResult(null);
        return result;
    }


}
